/**
 * 
 */
package messaage;

import java.util.ArrayList;
import java.util.List;

import utils.LogHelper;

import forum.*;
import forum.Thread;

/**
 * @author Olga
 * 
 */
public class GotThreadMsg extends Message {

	protected String threadContent;
	protected String forumID;
	protected forum.Thread thread;

	/**
	 * 
	 */
	public GotThreadMsg(String msg) {
		super.username = username;
		this.threadContent = msg;
		msg = msg.substring(10);
		this.parse(msg);
	}
	
	public GotThreadMsg(String uName, String forumID, Thread thread) {
		super();
		this.username = uName;
		this.forumID = forumID;
		this.thread = thread;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see messaage.Message#toString()
	 */
	@Override
	public String toString() {
		return "GotThread\n" +this.forumID +"\n"+ this.threadContent + "\n\0";
	}
	
	

	/**
	 * @param forumID the forumID to set
	 */
	public void setForumID(String forumID) {
		this.forumID = forumID;
	}

	private void parse(String msg) {

		List<Post> posts = new ArrayList<Post>();
		String[] splitted = msg.split("PostNode:\n");
		this.forumID = splitted[0].split("\n")[0];

		// splitted[1] contains the root post.
		// we can skip it, because it already shown

		
		for (int i = 1; i < splitted.length; i++) {

			String[] splittedfields = splitted[i].split("\n");
			
			// take postID
			int postID = Integer.parseInt(splittedfields[0]);

			// take author
			String author = splittedfields[1];

			// take date
			String dateString = splittedfields[2];
		
			// take parent post ID. here must returned -1. thus not used
			int parentID = Integer.parseInt(splittedfields[3]);

			// take subject
			String subject = splittedfields[4];

			// take content
			//the last line must be added without a newline character
			String content = "";
			for (int j = 5; j < splittedfields.length; j++) {
				if (j == splittedfields.length - 1)
					content += splittedfields[j];
				else
					content += splittedfields[j] + "\n";
			}

			// build post
			Post postToAdd = new Post(postID, subject, content, null, author);

			// set date to published date
			String date;
			try {
				date = dateString;
				postToAdd.setDate(date);
			} catch (Exception e) {
				LogHelper.instance().log(
						"Date parsing failed in parsing GotTopicsOfForumMsg");
			}
			
			// set parent post to newly created post
			// if (i == 1) this is the root post. It has no parent.
			// else, search parent in added posts. 
			if (i > 1)
				for (int j = posts.size() - 1; j >= 0; j--) 
					if (posts.get(j).getID() == parentID) {
						posts.get(j).addReply(postToAdd);
						break;
					}				
			// add post to list 
			posts.add(postToAdd);
		} // for
		
		this.thread = new Thread(1234, "Stam", "Stam", "Stam");
		//TODO: check why get(0) is null
		this.thread.setRoot(posts.get(0));
		
	} // parse

	/**
	 * @return the forumID
	 */
	public int getForumID() {
		return Integer.parseInt(forumID);
	}

	/**
	 * @return the thread
	 */
	public forum.Thread getThread() {
		return thread;
	}
	
	

}
